﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
//using System.Linq;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using System.Web.Services;
using System.Web.Services.Protocols;
using Dzzzr;
using System.Collections.Generic;

//using System.Xml.Linq;

namespace NWS
{
	/// <summary>
	/// Summary description for Reciever
	/// </summary>
	[WebService(Namespace = "http://tempuri.org/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[ToolboxItem(false)]
	// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
	// [System.Web.Script.Services.ScriptService]
	public class Reciever : System.Web.Services.WebService
	{

		/// <summary>
		/// Add point from <see cref="Marker"/>
		/// </summary>
		/// <param name="login">login</param>
		/// <param name="password">password</param>
		/// <param name="markerName"><see cref="Marker"/> name</param>
		/// <param name="gameName"><see cref="Game"/> name</param>
		/// <param name="latitude">Point latitude</param>
		/// <param name="longitude">Point logitude</param>
		/// <param name="altitude">Point altitude</param>
		/// <param name="speed">Point speed</param>
		[WebMethod]
		public void AddPoint2(string login, string password, string markerName, string gameName, float latitude, float longitude, float altitude, int speed)
		{
			if (!Membership.ValidateUser(login, password))
				throw new ApplicationException("Invalid login or password");
			MembershipUser user = Membership.GetUser(login);
			int userID = (int)user.ProviderUserKey;

			ProfileBase p = new ProfileBase();
			p.Initialize(login, true);
			int teamID = (int)p.GetPropertyValue("TeamID");

			Entity entity = Entity.GetEntityByName(teamID, markerName);
			if (entity == null)
				throw new ApplicationException("Entity is not found in the team");

			Game game = Game.GetGameByName(teamID, gameName);
			if (game == null)
				throw new ApplicationException("Game is not found in the team");

			Marker.AddPoint(entity.ID, game.ID, userID, latitude, longitude, altitude, speed);
		}
		/// <summary>
		/// Get last markers positions by game name
		/// </summary>
		/// <param name="gameName"></param>
		/// <param name="login"></param>
		/// <param name="password"></param>
		/// <returns></returns>
		[WebMethod]
		public List<Marker> GetMarkers(string login, string password, string gameName)
		{
			if (!Membership.ValidateUser(login, password))
				throw new ApplicationException("Invalid login or password");
			MembershipUser user = Membership.GetUser(login);
			int userID = (int)user.ProviderUserKey;

			ProfileBase p = new ProfileBase();
			p.Initialize(login, true);
			int teamID = (int)p.GetPropertyValue("TeamID");

			Game game = Game.GetGameByName(teamID, gameName);
			if (game == null)
				throw new ApplicationException("Game is not found in the team");

			return Marker.GetMarkerList(game.ID);
		}
	}
}
